home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / sbin / dhclient-script < prev    next >
Text File  |  2009-10-08  |  9KB  |  280 lines

  1. #!/bin/bash
  2.  
  3. # dhclient-script for Linux. Dan Halbert, March, 1997.
  4. # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
  5. # Modified for Debian.  Matt Zimmerman and Eloy Paris, December 2003
  6. # Modified to remove useless tests for antiquated kernel versions that
  7. # this doesn't even work with anyway, and introduces a dependency on /usr
  8. # being mounted, which causes cosmetic errors on hosts that NFS mount /usr
  9. # Andrew Pollock, February 2005
  10. # Modified to work on point-to-point links. Andrew Pollock, June 2005
  11. # Modified to support passing the parameters called with to the hooks. Andrew Pollock, November 2005
  12.  
  13. # The alias handling in here probably still sucks. -mdz
  14.  
  15. make_resolv_conf() {
  16.     if [ "$new_domain_name" -o "$new_domain_name_servers" ]; then
  17.         # Find out whether we are going to mount / rw
  18.         exec 9>&0 </etc/fstab
  19.         rootmode=rw
  20.         while read dev mnt type opts dump pass junk; do
  21.             [ "$mnt" != / ] && continue
  22.             case "$opts" in
  23.                 ro|ro,*|*,ro|*,ro,*)
  24.                    rootmode=ro
  25.                    ;;
  26.                  esac
  27.          done
  28.          exec 0>&9 9>&-
  29.  
  30.         # Wait for /etc/resolv.conf to become writable
  31.         if [ "$rootmode" = "rw" ]; then
  32.             while [ ! -w /etc ]; do
  33.                 sleep 0.1
  34.             done
  35.         fi
  36.  
  37.         local new_resolv_conf=/etc/resolv.conf.dhclient-new
  38.         rm -f $new_resolv_conf
  39.         if [ "$new_domain_name" ]; then
  40.             echo domain ${new_domain_name%% *} >>$new_resolv_conf
  41.         fi
  42.     if [ "$new_domain_search" ]; then
  43.         #new_domain_search="${new_domain_search% }"
  44.         if [ "$new_domain_name" ]; then
  45.         domain_in_search_list=""
  46.         for domain in "$new_domain_search"; do
  47.             if [ "$domain" = "$new_domain_name" ]; then
  48.                 domain_in_search="Yes"
  49.             fi
  50.         done
  51.         if [ ! "$domain_in_search" ]; then
  52.                 new_domain_search="$new_domain_name $new_domain_search"
  53.         fi
  54.         fi
  55.         echo "search ${new_domain_search}" >> $new_resolv_conf
  56.     elif [ "$new_domain_name" ]; then
  57.         echo "search ${new_domain_name}" >> $new_resolv_conf
  58.     fi
  59.         if [ "$new_domain_name_servers" ]; then
  60.                    for nameserver in $new_domain_name_servers; do
  61.                        echo nameserver $nameserver >>$new_resolv_conf
  62.             done
  63.         else # keep 'old' nameservers
  64.             sed -n /^\w*[Nn][Aa][Mm][Ee][Ss][Ee][Rr][Vv][Ee][Rr]/p /etc/resolv.conf >>$new_resolv_conf
  65.         fi
  66.         chown --reference=/etc/resolv.conf $new_resolv_conf
  67.         chmod --reference=/etc/resolv.conf $new_resolv_conf
  68.         mv -f $new_resolv_conf /etc/resolv.conf
  69.     fi
  70. }
  71.  
  72. run_hook() {
  73.     local script="$1"
  74.     local exit_status
  75.     shift    # discard the first argument, then the rest are the script's
  76.  
  77.     if [ -f $script ]; then
  78.         . $script "$@"
  79.     fi
  80.  
  81.  
  82.     if [ -n "$exit_status" ] && [ "$exit_status" -ne 0 ]; then
  83.         logger -p daemon.err "$script returned non-zero exit status $exit_status"
  84.         save_exit_status=$exit_status
  85.     fi
  86.  
  87.     return $exit_status
  88. }
  89.  
  90. run_hookdir() {
  91.     local dir="$1"
  92.     local exit_status
  93.     shift    # See run_hook
  94.  
  95.     if [ -d "$dir" ]; then
  96.         for script in $(run-parts --list $dir); do
  97.             run_hook $script "$@" || true
  98.             exit_status=$?
  99.         done
  100.     fi
  101.  
  102.     return $exit_status
  103. }
  104.  
  105. # Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
  106. exit_with_hooks() {
  107.     exit_status=$1
  108.  
  109.     # Source the documented exit-hook script, if it exists
  110.     if ! run_hook /etc/dhcp3/dhclient-exit-hooks "$@"; then
  111.         exit_status=$?
  112.     fi
  113.  
  114.     # Now run scripts in the Debian-specific directory.
  115.     if ! run_hookdir /etc/dhcp3/dhclient-exit-hooks.d "$@"; then
  116.         exit_status=$?
  117.     fi
  118.  
  119.     exit $exit_status
  120. }
  121.  
  122. set_hostname() {
  123.     local current_hostname=$(hostname)
  124.     if [ -z "$current_hostname" -o "$current_hostname" = "(none)" ]; then
  125.         hostname "$new_host_name"
  126.     fi
  127. }
  128.  
  129. if [ -n "$new_broadcast_address" ]; then
  130.     new_broadcast_arg="broadcast $new_broadcast_address"
  131. fi
  132. if [ -n "$old_broadcast_address" ]; then
  133.     old_broadcast_arg="broadcast $old_broadcast_address"
  134. fi
  135. if [ -n "$new_subnet_mask" ]; then
  136.     new_subnet_arg="netmask $new_subnet_mask"
  137. fi
  138. if [ -n "$old_subnet_mask" ]; then
  139.     old_subnet_arg="netmask $old_subnet_mask"
  140. fi
  141. if [ -n "$alias_subnet_mask" ]; then
  142.     alias_subnet_arg="netmask $alias_subnet_mask"
  143. fi
  144. # The 576 MTU is only used for X.25 and dialup connections
  145. # where the admin wants low latency.  Such a low MTU can cause
  146. # problems with UDP traffic, among other things.  As such,
  147. # disallow MTUs from 576 and below by default, so that broken
  148. # MTUs are ignored, but higher stuff is allowed (1492, 1500, etc).
  149. if [ -n "$new_interface_mtu" ] && [ $new_interface_mtu -gt 576 ]; then
  150.     mtu_arg="mtu $new_interface_mtu"
  151. fi
  152. if [ -n "$IF_METRIC" ]; then
  153.     metric_arg="metric $IF_METRIC"    # interfaces(5), "metric" option
  154. fi
  155.  
  156.  
  157. # The action starts here
  158.  
  159. # Invoke the local dhcp client enter hooks, if they exist.
  160. run_hook /etc/dhcp3/dhclient-enter-hooks
  161. run_hookdir /etc/dhcp3/dhclient-enter-hooks.d
  162.  
  163. # Execute the operation
  164. case "$reason" in
  165.     MEDIUM|ARPCHECK|ARPSEND)
  166.         # Do nothing
  167.         ;;
  168.     PREINIT)
  169.         # The DHCP client is requesting that an interface be
  170.         # configured as required in order to send packets prior to
  171.         # receiving an actual address. - dhclient-script(8)
  172.  
  173.         if [ -n "$alias_ip_address" ]; then
  174.             # Bring down alias interface. Its routes will disappear too.
  175.             ifconfig $interface:0- inet 0
  176.         fi
  177.         ifconfig $interface inet 0 up
  178.  
  179.         # We need to give the kernel some time to get the interface up.
  180.         sleep 1
  181.         ;;
  182.     BOUND|RENEW|REBIND|REBOOT)
  183.  
  184.         set_hostname
  185.  
  186.         if [ -n "$old_ip_address" -a -n "$alias_ip_address" -a \
  187.              "$alias_ip_address" != "$old_ip_address" ]; then
  188.             # Possible new alias. Remove old alias.
  189.             ifconfig $interface:0- inet 0
  190.         fi
  191.  
  192.         if [ -n "$old_ip_address" -a \
  193.              "$old_ip_address" != "$new_ip_address" ]; then
  194.             # IP address changed. Bringing down the interface will delete all routes,
  195.             # and clear the ARP cache.
  196.             ifconfig $interface inet 0
  197.  
  198.         fi
  199.  
  200.         if [ -z "$old_ip_address" -o "$old_ip_address" != "$new_ip_address" -o \
  201.             "$reason" = "BOUND" -o "$reason" = "REBOOT" ]; then
  202.  
  203.             ifconfig $interface inet $new_ip_address $new_subnet_arg \
  204.                 $new_broadcast_arg $mtu_arg
  205.  
  206.         # point to point
  207.         if [ "$new_subnet_mask" == "255.255.255.255" ]; then
  208.             for router in $new_routers; do
  209.                 route add -host $router dev $interface
  210.             done
  211.         fi
  212.  
  213.             for router in $new_routers; do
  214.                 route add default dev $interface gw $router $metric_arg
  215.             done
  216.         fi
  217.  
  218.         if [ "$new_ip_address" != "$alias_ip_address" -a -n "$alias_ip_address" ];
  219.             then
  220.             ifconfig $interface:0- inet 0
  221.             ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  222.             route add -host $alias_ip_address $interface:0
  223.         fi
  224.  
  225.         make_resolv_conf
  226.  
  227.         ;;
  228.  
  229.     EXPIRE|FAIL|RELEASE|STOP)
  230.         if [ -n "$alias_ip_address" ]; then
  231.             # Turn off alias interface.
  232.             ifconfig $interface:0- inet 0
  233.         fi
  234.  
  235.         if [ -n "$old_ip_address" ]; then
  236.             # Shut down interface, which will delete routes and clear arp cache.
  237.             ifconfig $interface inet 0
  238.         fi
  239.  
  240.         if [ -n "$alias_ip_address" ]; then
  241.             ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  242.             route add -host $alias_ip_address $interface:0
  243.         fi
  244.  
  245.         ;;
  246.  
  247.     TIMEOUT)
  248.         if [ -n "$alias_ip_address" ]; then
  249.             ifconfig $interface:0- inet 0
  250.         fi
  251.  
  252.         ifconfig $interface inet $new_ip_address $new_subnet_arg \
  253.             $new_broadcast_arg $mtu_arg
  254.  
  255.         set -- $new_routers
  256.         first_router="$1"
  257.  
  258.         if [ -z "$first_router" ] || ping -q -c 1 $first_router; then
  259.             if [ "$new_ip_address" != "$alias_ip_address" -a \
  260.                 -n "$alias_ip_address" ]; then
  261.                 ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  262.                 route add -host $alias_ip_address dev $interface:0
  263.             fi
  264.         
  265.             for router in $new_routers; do
  266.                 route add default dev $interface gw $router $metric_arg
  267.             done
  268.  
  269.             make_resolv_conf
  270.         else
  271.             # Changed from 'ifconfig $interface inet 0 down' - see Debian bug #144666
  272.             ifconfig $interface inet 0
  273.             exit_with_hooks 2 "$@"
  274.         fi
  275.  
  276.         ;;
  277. esac
  278.  
  279. exit_with_hooks 0
  280.